package com.ccg.plat.presentation.ui.feature5.view

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import coil3.compose.AsyncImage
import com.ccg.plat.presentation.ui.feature5.viewmodel.VideoListViewModel
import com.ccg.plat.ui.theme.AndroidVideoPlayerClientTheme
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * @author : C4_雍和
 * 描述 :
 * 主要功能 :
 * 维护人员 : C4_雍和
 * date : 2025/8/9 8:52
 */
class VideoListActivity : ComponentActivity() {
    private val context = this
    private val viewModel by lazy { ViewModelProvider(this)[VideoListViewModel::class.java] }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        lifecycleScope.launch(Dispatchers.IO) {
            viewModel.initData(intent.getStringExtra("url"))
        }
        enableEdgeToEdge()
        setContent {
            AndroidVideoPlayerClientTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    var cIndex by remember { mutableIntStateOf(-1) }
                    LazyColumn(
                        modifier = Modifier
                            .fillMaxSize()
                            .padding(innerPadding), horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        if (viewModel.uiState.tips.isNotEmpty()) {
                            item {
                                Text(text = viewModel.uiState.tips, modifier = Modifier.padding(start = 20.dp, top = 15.dp, bottom = 15.dp), fontSize = 20.sp)
                            }
                        }

                        item {
                            Column(
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .wrapContentHeight(), horizontalAlignment = Alignment.CenterHorizontally
                            ) {
                                Spacer(modifier = Modifier.height(10.dp))
                                Button(onClick = {
                                    lifecycleScope.launch(Dispatchers.IO) {
                                        viewModel.newsChannel.send(VideoListViewModel.ContentIntent.GoRandomVideoPlayer(activity = context))
                                    }
                                }) {
                                    Text(text = "  随机播放  ")
                                }
                                Spacer(modifier = Modifier.height(10.dp))
                            }
                        }

                        items(count = viewModel.uiState.uiListData.size) { index ->
                            Column(
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .wrapContentHeight()
                                    .clickable {
                                        lifecycleScope.launch(Dispatchers.IO) {
                                            cIndex = index
                                            viewModel.newsChannel.send(VideoListViewModel.ContentIntent.GoVideoPlayerActivity(activity = context, data = viewModel.uiState.uiListData[index]))
                                        }
                                    }) {
                                Spacer(modifier = Modifier.height(5.dp))
                                Row(
                                    modifier = Modifier
                                        .fillMaxWidth()
                                        .wrapContentHeight()
                                        .padding(horizontal = 12.dp), verticalAlignment = Alignment.CenterVertically
                                ) {
                                    Spacer(modifier = Modifier.height(10.dp))
                                    AsyncImage(
                                        model = viewModel.uiState.uiListData[index].pUrl, contentDescription = null, modifier = Modifier
                                            .width(120.dp)
                                            .wrapContentHeight()
                                            .clip(shape = RoundedCornerShape(10))
                                    )
                                    Spacer(modifier = Modifier.width(10.dp))
                                    Column(
                                        modifier = Modifier
                                            .weight(1f)
                                            .wrapContentHeight()
                                    ) {
                                        Text(
                                            text = "$index" + viewModel.uiState.uiListData[index].name, color = if (index == cIndex) {
                                                Color.Blue
                                            } else {
                                                Color.Unspecified
                                            }, modifier = Modifier.wrapContentSize()
                                        )
                                    }
                                    Spacer(modifier = Modifier.width(10.dp))
                                    Button(onClick = {
                                        lifecycleScope.launch(Dispatchers.IO) {
                                            viewModel.newsChannel.send(VideoListViewModel.ContentIntent.RemoveVideoForIndex(index = index))
                                        }
                                    }) {
                                        Text(text = "删除")
                                    }
                                }
                                Spacer(modifier = Modifier.height(5.dp))
                                HorizontalDivider(thickness = 1.dp)
                            }
                        }
                    }
                }
            }
        }
    }
}